﻿var HOST = 'http://192.168.128.128:4225/controller/Skill.ashx?action=';
var HOSTManager = 'http://192.168.128.128:4225/controller/SkillManager.ashx?action=';
var api = {
    Skill: {
        GetEduPlanContent: HOST + 'GetEduPlanContent',
        GetBindUser: HOST + 'GetBindUser',
        BindUser: HOST + 'BindUser',
        GetTest: HOST + 'GetTest',
        GetTestFree: "test.json",
        AppLogin:HOST+"AppLogin",
        AppAginLogin:HOST+"AppAginLogin",
    },
    SkillManager: {
        LoginUser: HOSTManager + 'LoginUser',
        GetAssginAgentList: HOSTManager + 'GetAssginAgentList',
        GetDrvingSchool: HOSTManager + 'GetDrvingSchool',
        GetDrvingListByAgentID: HOSTManager + 'GetDrvingListByAgentID',
        AssignrvingSchool: HOSTManager + 'AssignrvingSchool',
        getTempUserList: HOSTManager + 'getTempUserList',
        Get_DrvingList: HOSTManager + 'Get_DrvingList',

        get_WxReport: HOSTManager + 'get_WxReport',
        Online_Pay: HOSTManager + 'Online_Pay',
    }
};
/*
 * 用户相关的方法及属性
 */
var Skill = {
    //获取章节列表
    GetEduPlanContent: function (params, suc, err) {
        ajax.post(api.Skill.GetEduPlanContent, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    },
    //获取微信对应的绑定用户
    GetBindUser: function (params, suc, err) {
        ajax.post(api.Skill.GetBindUser, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    },
    //微信绑定用户
    BindUser: function (params, suc, err) {
        ajax.post(api.Skill.BindUser, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    },
    //获取试题
    GetTest: function (params, suc, err) {
        ajax.post(api.Skill.GetTest, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    },
    //获取免费试用试题
    GetTestFree: function (params, suc, err) {
        ajax.post(api.Skill.GetTestFree, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    }, 
     //App登录
    AppLogin: function (params, suc, err) {
        ajax.post(api.Skill.AppLogin, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    }, 
    //app自动登录
    AppAginLogin: function (params, suc, err) {
        ajax.post(api.Skill.AppAginLogin, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    }, 
};

var SkillManager = {
    //登录
    LoginUser: function (params, suc, err) {
        ajax.post(api.SkillManager.LoginUser, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    },
    //获取公司分发给代理商的激活卡 列表
    GetAssginAgentList: function (params, suc, err) {
        ajax.post(api.SkillManager.GetAssginAgentList, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    },
    //获取公司分发给代理商的激活卡 明细
    GetDrvingSchool: function (params, suc, err) {
        ajax.post(api.SkillManager.GetDrvingSchool, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    },
    //根据代理商获取驾校列表
    GetDrvingListByAgentID: function (params, suc, err) {
        ajax.post(api.SkillManager.GetDrvingListByAgentID, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    },
    //代理商分发激活卡给驾校
    AssignrvingSchool: function (params, suc, err) {
        ajax.post(api.SkillManager.AssignrvingSchool, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    },
    //获取激活卡列表
    getTempUserList: function (params, suc, err) {
        ajax.post(api.SkillManager.getTempUserList, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    },
    //获取代理商分发驾校的列表
    Get_DrvingList: function (params, suc, err) {
        ajax.post(api.SkillManager.Get_DrvingList, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    },
    //获取分发和激活报表
    get_WxReport: function (params, suc, err) {
        ajax.post(api.SkillManager.get_WxReport, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    },

    //在线支付激活
    Online_Pay: function (params, suc, err) {
        ajax.post(api.SkillManager.Online_Pay, params,
			function (data, textStatus, xhr) {
			    suc(data);
			});
    },
    
};

/* 
 * 保存数据
 * 注：目前使用h5的localStorage, 只能非跨域下工作
 */
function cache(key, val) {
    if (key === 'clear') {
        window.localStorage.clear();
    } else if (val === null) {
        window.localStorage.removeItem(key);
    } else if (val) {
        window.localStorage[key] = val;
    } else {
        return window.localStorage[key];
    }
}
/*
 * 数据访问
 */
var persistent = {
    idcard: function () {
        return cache('idcard');
    },
    openid: function () {
        return cache('openid');
    },
    clear: function () {
        //清空所有缓存
        cache("clear");
        cache('openid', "");
        cache('idcard', "");
    }
}



/*
 * message常量
 */
var msg = {
    GOTOHOME: 'zzxc',
    NETWORK_IS_UNAVAILABLE: "网络连接失败，请检查您的网络！",
    LOAD_DATA_FAIL: '加载数据失败，请等一会再试！',
    AUTH_EXPIRED: '认证信息失效，请重新登录！',
    SERVER_ERROR: '服务响应失败！',
};

/*
 * 工具类
 */
var tool = {
    isEmptyVal: function (val) {
        if (val === null || typeof (val) === 'undefined' || val === '')
            return true;

        return false;
    },
    isFunction: function (f) {
        return Object.prototype.toString.call(f) === "[object Function]";
    },
    isObject: function (o) {
        return Object.prototype.toString.call(o) === "[object Object]";
    },
    isString: function (s) {
        return Object.prototype.toString.call(s) === "[object String]";
    }
};



var animation = {
    popIn: 'pop-in',
    popOut: 'pop-out',
    slideInRight: 'slide-in-right',
    slideOutRight: 'slide-out-right',
    slideInBottom: 'slide-in-bottom',
    slideOutBottom: 'slide-out-bottom',
}
/*
 * 封装常用的打开窗口，一般只需传入url及id
 * 默认显示等待框，默认不自动显示
 */

function openPage(url, id, extraObj, styleObj, showObj, waitObj, extrasObj) {
    showObj = showObj || {
        autoShow: true
    };
    waitObj = waitObj || {
        autoShwo: true
    }; // 默认显示等待框

    extrasObj = extrasObj || {};

    mui.openWindow({
        url: url,
        id: id,
        styles: styleObj,
        extras: extraObj,
        show: showObj,
        waiting: waitObj
    });
}

function showPage(wv, aniShow, closeWaiting, duration) {
    if (tool.isEmptyVal(wv)) {
        console.log('webview为空');
        return;
    }
    aniShow = aniShow || animation.popIn;
    duration = duration || (mui.os.ios ? 300 : 250); // pop-in动画在ios平台上300ms为佳，在android平台上250为佳
    wv.show(aniShow, duration);
    closeWaiting = typeof (closeWaiting) === "undefined" ? true : closeWaiting; // 默认要关闭
    if (closeWaiting) plus.nativeUI.closeWaiting();
}



var ajaxStatus = {
    OK: 200,
    ERROR: 300,
    FAIL_AUTH: 400,
};

/*
 * 封装ajax
 */
var ajax = {
    post: function (url, paramObj, suc, wait, comp, err) {
        if (tool.isObject(url)) {
            paramObj = url.paramObj;
            suc = url.suc;
            wait = url.wait;
            comp = url.comp;
            err = url.err;
            url = url.url;
        }

        wait = wait === null ? false : wait;


        var headerObj = {};

        // 发送请求
        mui.ajax(url+"&r="+Math.random(), {
            type: 'POST',
            timeout: 10000,
            data: paramObj,
            dataType: 'json',
            headers: headerObj,
            success: function (data, textStatus, xhr) {
                if (textStatus == 'success') {
                    if (tool.isFunction(suc)) {
                        suc(data, textStatus, xhr);
                    }
                } else {
                    var errMsg = data.msg || msg.LOAD_DATA_FAIL;
                }
            },
            error: function (xhr, type) {
                if (tool.isFunction(err)) err();
            },
            complete: function () {
                if (tool.isFunction(comp)) comp();
            }
        });
    },
   };

//格式化时间
function formatHous(val) {
    var intVal = parseInt(val)
    if (intVal == val) {
        return intVal + ':00';
    }
    return intVal + ':30';
}

var System={
	checkAppUpdate: function(ifUpdate,androidDownUrl) {
		if(ifUpdate) {
						var dtask = plus.downloader.createDownload(androidDownUrl, {
							method: "GET"
						}, function(d, status) {
							if(status == 200) {
								plus.runtime.install(d.filename, {
									force: true
								}, function() {
									console.log("ok");
								}, function(e) {
									sys.AppUpdateError(e.message);
								});
							} else {

							}
						});
						dtask.addEventListener('statechanged', function(d, status) {
							console.log("statechanged: " + d.state);
						});
						dtask.start();
					}


	},
	AppUpdateError: function(errMsg, suc) {
		

	},	
};